package com.Wheesper.www.WheesperServer.Model;

import java.util.HashMap;
import java.util.Map;

import com.wheesper.www.WheesperServer.DAO.UserInfoDAO;

public class UserInfoCenter{
	private static UserInfoCenter _instance = new UserInfoCenter();
	
	public static UserInfoCenter Instance() {
		return _instance;
	}
	public UserInfoCenter() {
		_user2info = new HashMap<User,UserInfo>();
		loadData();//从存档中读取UserInfo信息
	}
	
	private void loadData() {
		//TODO:
	}
	
	private Map<User,UserInfo> _user2info;
	private UserInfo Query(User user) {
		if(_user2info.containsKey(user)) {
			return _user2info.get(user);
		}else {
			return UserInfoDAO.Instance().Query(user);
		}
	}
	private void Modify(UserInfo info) {
		User user = UserCenter.Instance().UserObject(info.ID());
		_user2info.put(user, info);
		UserInfoDAO.Instance().Update(info);
	}
	
	public UserInfo UserInfo(String email) {
		User user = UserCenter.Instance().UserObject(email);
		UserInfo info = Query(user);
		return info;
	}
	
	public void modify(UserInfo info) {
		Modify(info);
	}
	
	public void create(User user,String nickname) {
		UserInfo info = new UserInfo(user.ID());
		info.NickName(nickname);
		_user2info.put(user, info);
		UserInfoDAO.Instance().Create(info);
	}
}
